package rexsee.core.browser.clazz;

import java.util.ArrayList;

import rexsee.core.application.RexseeApplication;
import rexsee.core.browser.Browser;
import android.content.Context;
import android.net.Uri;

public class DomainWhiteList {
	private final ArrayList<String> list;
	private final Context mContext;
	private Browser mBrowser = null;
	public DomainWhiteList(Browser browser) {
		this(browser.getContext());
		mBrowser = browser;
	}
	public DomainWhiteList(Context context) {
		mContext = context;
		list = new ArrayList<String>();
	}
	public ArrayList<String> getList() {
		return list;
	}
	private void add(String domain) {
		if (domain != null && !domain.equals("")) {
			list.add(domain);
		}
	}
	private void add(String[] domains) {
		if (domains != null) {
			for (int i = 0; i < domains.length; i++)
				list.add(domains[i]);
		}
	}
	public void inherit() {
		if (mBrowser == null) {
			RexseeApplication application = new RexseeApplication(mContext);
			add(Uri.parse(application.getCurrentHome()).getHost());
			add(application.resources.domainWhiteList);
			return;
		} else {
			if (mBrowser.parentBrowser == null) {
				add(Uri.parse(mBrowser.application.getCurrentHome()).getHost());
				add(mBrowser.application.resources.domainWhiteList);
			} else {
				DomainWhiteList appendList = mBrowser.parentBrowser.domainWhiteList;
				list.clear();
				if (appendList != null) {
					for (int i = 0; i < appendList.list.size(); i++) {
						this.add(appendList.list.get(i));
					}
				}
			}
		}
	}
	public boolean contains(String domain) {
		return list.contains(domain);
	}
	public boolean isValidUrl(String url) {
		if (list.size() <= 1) return true;
		else return list.contains(Uri.parse(url).getHost());
	}
}
